//
//  JYOrderController.m
//  JYFarm
//
//  Created by 花花 on 2017/10/16.
//  Copyright © 2017年 花花. All rights reserved.
//

#import "JYOrderController.h"
#import "JYOrderHeaderView.h"
#import "JYOrderAllController.h"
#import "JYOrderPayController.h"
#import "JYOrderShippingController.h"
#import "JYOrderGoodsController.h"
#import "JYOrderCompleteController.h"
@interface JYOrderController ()<UIScrollViewDelegate>

@property(nonatomic)JYOrderHeaderView *headerView; //headerView

@property(nonatomic)UIScrollView *scrollView;;

@property(nonatomic)NSArray *titleArr; //数组

@end

@implementation JYOrderController

#pragma mark - Life Cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的订单";
  
    [self setupSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    if (self.index) {
        
        [self changeScrollview:self.index];
        
    }
}

#pragma mark - setupSubView
- (void)setupSubView{


    [self.view addSubview:self.headerView];

    [self.view addSubview:self.scrollView];
    
    [self addViewControlls];
    
}
#pragma mark - addViewControlls
- (void)addViewControlls{

    //控制器的名字
    NSArray *ControllerViews = @[@"JYOrderAllController",@"JYOrderPayController",
                                 @"JYOrderShippingController",@"JYOrderGoodsController",
                                 @"JYOrderCompleteController"];

    for (int i = 0; i< ControllerViews.count; i++){
        
    
        Class class = NSClassFromString(ControllerViews[i]);
      
        JYBaseOrderController * viewController  = [[class alloc] init];
        viewController.type = i;
        viewController.view.frame = CGRectMake(i * Screen_Width, 0, Screen_Width, _scrollView.height);
        
        [_scrollView addSubview:viewController.view];
        
        [self addChildViewController:viewController];
        
    }
}


#pragma mark - <UIScrollViewDelegate>
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
    [_headerView setSelectAtIndex:index];
    
    
}


#pragma mark - Custom Methods
-(void)adjustScrollView:(NSInteger)index{
    [UIView animateWithDuration:0.2 animations:^{
        _scrollView.contentOffset = CGPointMake(index*_scrollView.bounds.size.width, 0);
    }];
}

-(void)changeScrollview:(NSInteger)index{
    
    [UIView animateWithDuration:0 animations:^{
        _scrollView.contentOffset = CGPointMake(index*_scrollView.bounds.size.width, 0);
    }];
    
}


#pragma mark - Setter && Getter Methods
- (JYOrderHeaderView *)headerView{
    
    if (!_headerView) {
        JYWeakSelf;
        _headerView = [[JYOrderHeaderView alloc]initWithFrame:CGRectMake(0, 0,Screen_Width, 40)];
        _headerView.backgroundColor =[UIColor whiteColor];
        _headerView.itemsArr = self.titleArr;
        _headerView.itemHandlerOnClick = ^(NSInteger index){
            
            [weakSelf adjustScrollView:index];
            
        };
    }
    return _headerView;
    
}

- (UIScrollView *)scrollView{
    
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_headerView.frame),Screen_Width,Screen_Height-64-40)];
        
        _scrollView.contentSize = CGSizeMake(_scrollView.bounds.size.width*self.titleArr.count, _scrollView.bounds.size.height);
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
        _scrollView.directionalLockEnabled = YES;
    }
    
    return _scrollView;
}
- (NSArray *)titleArr{

    if (!_titleArr) {
        _titleArr = @[@"全部",@"待付款",@"待发货",@"待收货",@"已完成"];
    }
    return _titleArr;

}



@end
